
package gui;

import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collections;
import java.util.List;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

import model.Tec;
import parser.MascaraFactory;
import util.Util;

public class ParametrosGui extends JDialog implements ActionListener
{
   
   private static ParametrosGui This; 
   
   private List<String> dados = Collections.EMPTY_LIST;
   
   TableModel model;
   JTable table;
   JButton botaoAdd, botaoRem, botaoShw;
   JButton botaoClose, botaoOk;
   
   JComboBox acoesCB;
   
   JTextField chavesTF;
   
   
   
   public ParametrosGui(Frame pai, boolean modal)
   {
      super(pai, modal);
      initComponents();
   }
   
   
   public static ParametrosGui getInstance(Frame pai, boolean modal)
   {
      if(This == null)
         This = new ParametrosGui(pai, modal);
      return This;
   }

   private void initComponents()
   {
      setLayout(new BorderLayout());
      
      acoesCB  = new JComboBox ( MascaraFactory.comandos.get(Tec.EWSD).keySet().toArray() );
      chavesTF = new JTextField( 20                                                       );
      
      
      JPanel panelTop = new JPanel(new GridLayout(3,1));
      panelTop.add(acoesCB);
      panelTop.add(chavesTF);
      
      add(panelTop, BorderLayout.NORTH);
      
      model = new TableModel();
      table = new JTable(model);
      
      add(new JScrollPane(table), BorderLayout.CENTER);
      
      botaoAdd = new JButton();
//      botaoAdd.setPreferredSize(Constantes.DIM);
      botaoAdd.setIcon(Util.createImageIcon("images/ins.png"));
      botaoAdd.addActionListener(new ActionListener()
      {
         @Override
         public void actionPerformed(ActionEvent e)
         {
            adicionarNovaLinha(e);
         }
      });
      
      botaoRem = new JButton();
//      botaoRem.setPreferredSize(Constantes.DIM);
      botaoRem.setIcon(Util.createImageIcon("images/del.png"));
      botaoRem.addActionListener(new ActionListener()
      {
         @Override
         public void actionPerformed(ActionEvent e)
         {
            removerLinhas(e);
         }
      });
      
      botaoShw = new JButton();
//      botaoShw.setPreferredSize(Constantes.DIM);
      botaoShw.setIcon(Util.createImageIcon("images/lupa.png"));
      botaoShw.addActionListener(new ActionListener()
      {
         @Override
         public void actionPerformed(ActionEvent e)
         {
            print(e);
         }
      });
 
      
      
    Box box = Box.createVerticalBox();
    
//    JPanel box = new JPanel(new FlowLayout(FlowLayout.CENTER));
    box.add( botaoAdd );   
    box.add( botaoRem );   
    box.add( botaoShw ); 
    
  add(box, BorderLayout.EAST);
    
//       panelTop.add(box);
    
      
      botaoClose = new JButton("Fechar");
      botaoClose.setPreferredSize(Constantes.DIM);
      botaoClose.setIcon(Util.createImageIcon("images/cancel.png"));
      botaoClose.addActionListener(this);
      
      botaoOk = new JButton("Ok");
      botaoOk.setPreferredSize(Constantes.DIM);
      botaoOk.setIcon(Util.createImageIcon("images/add.png"));
      botaoOk.addActionListener(this);
      
      JPanel panelBot = new JPanel();
      panelBot.add(botaoClose);
      panelBot.add(botaoOk);
      add(panelBot, BorderLayout.SOUTH);
      
      
      setSize(400,300);
   }
   




   private void print(ActionEvent e)
   {
      for (String s : model.getParametros())
      {
         System.out.println(s);
      }
   }




   private void removerLinhas(ActionEvent e)
   {
      model.removeAll( table.getSelectedRows() );
   }


   private void limparFormulario()
   {
      chavesTF.setText("");
      model.getParametros().removeAllElements();
   }
   
   

   private void adicionarNovaLinha(ActionEvent e)
   {
      model.criarNovaLinha("");
   }


   public List<String> getDados()
   {
      return dados;
   }
   
   public String getChaves()
   {
      return chavesTF.getText();
   }
   
   

   /**
    * @param args
    */
   public static void main(String[] args)
   {
      new ParametrosGui(null, false).setVisible(true);
   }


   @Override
   public void actionPerformed(ActionEvent e)
   {
      if(e.getSource() == botaoOk)
      {
         dados = model.getParametros();
         dispose();
      }
      else if(e.getSource() == botaoClose)
      {
    	 limparFormulario();
         dispose();
      }
   }


   public String getAcao()
   {
      return acoesCB.getSelectedItem().toString();
   }

}
